package com.sihan.framework.etm.service.workTicket.impl;


import com.framework.model.ApiResult;
import com.framework.model.PageResult;
import com.framework.mybatis.service.impl.BaseServiceImpl;
import com.framework.util.PageUtils;
import com.framework.util.WebUtils;
import com.github.pagehelper.PageHelper;
import com.sihan.framework.etm.common.util.DateUtils;
import com.sihan.framework.etm.dto.WorkListDTO;
import com.sihan.framework.etm.dto.WorkTicketDTO;
import com.sihan.framework.etm.entity.Org;
import com.sihan.framework.etm.entity.WorkTicket;
import com.sihan.framework.etm.entity.baseData.DeviceModel;
import com.sihan.framework.etm.mapper.OrgMapper;
import com.sihan.framework.etm.mapper.WorkTicketMapper;
import com.sihan.framework.etm.mapper.baseData.DeviceMapper;
import com.sihan.framework.etm.service.workTicket.WorkTicketService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class WorkTicketServiceImpl extends BaseServiceImpl<WorkTicket, String> implements WorkTicketService {

    @Autowired
    private WorkTicketMapper workTicketMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private OrgMapper orgMapper;



    /**
     * 分页获取票据信息
     * @param dto
     * @return
     */
    @Override
    public ApiResult<PageResult<WorkTicket>> listWorkTicket(WorkListDTO dto) {
        PageHelper.startPage(dto);
        List<WorkTicket> workTickets = workTicketMapper.listWorkTicket(dto);
        PageResult<WorkTicket> res = PageUtils.toPageResult(workTickets);
        return ApiResult.success(res);
    }

    /**
     * 添加票据
     * @param dto
     * @return
     */
    @Override
    public ApiResult<?> addWorkList(WorkTicketDTO dto) {
        WorkTicket workTicket = new WorkTicket();
        BeanUtils.copyProperties(dto,workTicket);
        String currentDate = DateUtils.getInstance().getCurrentDate(DateUtils.getInstance().SDF_YM_DD3);
        //生成票据号
        currentDate = currentDate+makeNum();
        workTicket.setTicket(currentDate);
        //添加时不设置时间 默认创建时间
        if (null == workTicket.getStartTime()){
            workTicket.setStartTime(new Date());
        }else {
            Date dateByStr = DateUtils.getInstance().getDateByStr(dto.getStartTime().toString(), DateUtils.getInstance().SDF_HH);
            workTicket.setStartTime(dateByStr);
        }

        if (null != dto.getOrg()){
            Org org = orgMapper.selectById(dto.getOrg());
            dto.setOrgName(org.getName());
        }else {
            return ApiResult.fail("请选择所属组织!");
        }
        workTicket.setState(1);
        workTicket.setCreateTime(new Date());
        workTicket.setCreateUser(WebUtils.username());
        workTicket.setIsDelete(false);
        if (null != dto.getIds() && dto.getIds().size() > 0){
            for (String str : dto.getIds()){
                if(workTicketMapper.whetherBinding(str) > 0){
                    DeviceModel deviceModel = deviceMapper.selectById(str);
                    return ApiResult.fail(deviceModel.getName()+"已绑定其他工作票！");
                }
            }
            workTicketMapper.insertBatchBoolean(dto.getIds(),currentDate);
        }
        workTicketMapper.insert(workTicket);
        return ApiResult.success("添加成功");
    }

    /**
     * 根据 TicketCode 获取 详情
     * @param ticketCode
     * @return
     */
    @Override
    public ApiResult<WorkTicketDTO> findByTicketCode(String ticketCode) {
        WorkTicketDTO dto = workTicketMapper.findByTicketCode(ticketCode);
        return ApiResult.success(dto);
    }

    /**
     *
     * @param devId
     * @return
     */
    @Override
    public WorkTicketDTO findByDevId(String devId) {
        return workTicketMapper.findByDevId(devId);
    }

    /**
     * 根据 工作票修改状态
     * @param towerCode
     * @param state
     * @return
     */
    @Override
    public ApiResult<?> updateTowerCode(String towerCode, Integer state) {
        WorkTicket workTicket = workTicketMapper.selectByTowerCode(towerCode);
        if (null != workTicket){
            workTicket.setState(state);
            workTicket.setUpdateTime(new Date());
            workTicket.setCreateUser(WebUtils.username());
            if (2 == state){
                workTicket.setEndTime(new Date());
            }
            workTicketMapper.updateById(workTicket);
            return ApiResult.success("修改成功");
        }
        return ApiResult.fail();
    }


    // 生成6位随机数
    private String makeNum() {
        Random random = new Random();
        //生成一个随机数[0,999999) 末尾加上“”表明是字符串类型
        String num = random.nextInt(999999) + "";
        StringBuffer stringBuffer = new StringBuffer();
        //不足7位用0填充
        for (int i = 0; i < 6 - num.length(); i++) {
            stringBuffer.append("0");
        }
        num = stringBuffer + num;
        return num;
    }
}
